package cn.easybuy.servlet.users;

import cn.easybuy.entity.User;
import cn.easybuy.service.UserService;
import cn.easybuy.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/userselectServlet")
public class UserselectServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            request.setCharacterEncoding("utf-8");
            User u = new User();
            String name = request.getParameter("loginName");
            String pwd = request.getParameter("password");
            u.setLoginName(name);
            u.setPassword(pwd);
            User user = userService.checkLogin(u);
            if (user!=null){
                request.setAttribute("msg","登录成功");
                HttpSession session = request.getSession();
                session.setAttribute("sessionUser",user);
                request.getRequestDispatcher("/productCateServlet").forward(request,response);
            }else {
                request.setAttribute("msg","用户名或密码错误");
                request.getRequestDispatcher("pre/login.jsp").forward(request,response);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
